/*
 *接口编写处...
*如果接口需要做Action的权限验证，请在Action上使用属性
*如: [ApiActionPermission("equip_devcatalog",Enums.ActionPermissionOptions.Search)]
 */
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using VOL.Entity.DomainModels;
using MES.Equipment.IServices;
using VOL.Core.BaseProvider;
using VOL.Entity.DTO.Equip;
using VOL.Core.Configuration;
using VOL.Core.Enums;
using VOL.Core.Filters;
using VOL.Core.ManageUser;
using VOL.System.Repositories;
using MES.Equipment.IRepositories;
using VOL.Core.Extensions;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using MES.Equipment.Repositories;
using Microsoft.AspNetCore.Authorization;

namespace MES.Equipment.Controllers
{
    public partial class equip_devcatalogController
    {
        private readonly Iequip_devcatalogService _service;//访问业务代码
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly Iequip_deviceService _deviceservice;
        private readonly Iequip_devcatalogRepository _repository;//访问数据库

        [ActivatorUtilitiesConstructor]
        public equip_devcatalogController(
            Iequip_devcatalogService service,
            IHttpContextAccessor httpContextAccessor
            ,
            Iequip_deviceService decice,
            Iequip_devcatalogRepository repository

        )
        : base(service)
        {
            _service = service;
            _httpContextAccessor = httpContextAccessor;
            
            _deviceservice = decice;
            _repository = repository;
        }

        /// <summary>
        /// 获取树形
        /// </summary>
        /// <param name="pId"></param>
        /// <returns></returns>
        [HttpGet, Route("GetEquipTree"),AllowAnonymous]
        public IActionResult GetEquipTree(string? pId)
        {
            var list = _service.GetEquipTree(pId);
            return Ok(list);
        }
  
        public override ActionResult GetPageData([FromBody] PageDataOptions loadData)
        {
            //没有查询条件显示所有一级节点数据
            if (loadData.Value.GetInt() == 1)
            {
                return GetTreeTableRootData(loadData).Result;
            }
            //有查询条件使用框架默认的查询方法
            return base.GetPageData(loadData);
        }

        /// <summary>
        /// treetable 获取子节点数据(2021.05.02)
        /// </summary>
        /// <param name="loadData"></param>
        /// <returns></returns>
        //[ApiActionPermission(ActionPermissionOptions.Search)]
        //[HttpPost, Route("GetPageData")]
        //public override ActionResult GetPageData([FromBody] PageDataOptions loadData)
        //{
        //    return GetTreeTableRootData(loadData).Result;
        //}

        /// <summary>
        /// treetable 获取一级(根)节点数据
        /// </summary>
        /// <returns></returns>
        [HttpPost, Route("getTreeTableRootData")]
        [ApiActionPermission(ActionPermissionOptions.Search)]
        public async Task<ActionResult> GetTreeTableRootData([FromBody] PageDataOptions options)
        {
            //页面加载根节点数据条件x => x.ParentId == 0,自己根据需要设置
            //var dbServiceId = UserContext.CurrentServiceId;
            var query = _repository.FindAsIQueryable(x => x.ParentId == " ");

            var queryChild = _repository.FindAsIQueryable(x => true);
                                var rows = await query.TakeOrderByPage(options.Page, options.Rows)
                .OrderBy(x => x.DevCatalogId).Select(s => new
                {

                    s.DevCatalogId,
                    s.DevCatalogName,
                    s.DevCatalogCode,
                    s.ParentId,
                    s.Enable,
                    s.Remark,
                    s.CreateID,
                    s.Creator,
                    s.CreateDate,
                    s.ModifyID,
                    s.Modifier,
                    s.ModifyDate,
                    hasChildren = queryChild.Any(x => x.ParentId == s.ParentId)
                }).ToListAsync();
            return JsonNormal(new { total = await query.CountAsync(), rows });
        }

        /// <summary>
        ///treetable 获取子节点数据
        /// </summary>
        /// <returns></returns>
        [HttpPost, Route("getTreeTableChildrenData")]
        [ApiActionPermission(ActionPermissionOptions.Search)]
        public async Task<ActionResult> GetTreeTableChildrenData(string roleId)
        {
            //点击节点时，加载子节点数据
            var roleRepository = equip_devcatalogRepository.Instance.FindAsIQueryable(x => true);
            var query = roleRepository.Where(x => x.ParentId == roleId);
            //if (AppSetting.UseDynamicShareDB)
            //{
            //    query = query.Where(x => x.DbServiceId == UserContext.CurrentServiceId);
            //}
            var rows = await query
                .Select(s => new
                {
                    s.DevCatalogId,
                    s.DevCatalogName,
                    s.DevCatalogCode,
                    s.ParentId,
                    s.Enable,
                    s.Remark,
                    s.CreateID,
                    s.Creator,
                    s.CreateDate,
                    s.ModifyID,
                    s.Modifier,
                    s.ModifyDate,
                    hasChildren = roleRepository.Any(x => x.ParentId == s.ParentId)
                }).ToListAsync();
            return JsonNormal(new { rows });
        }

        ///// treetable 获取根节点数据
        ///// </summary>
        ///// <returns></returns>
        //[HttpPost, Route("getCatalogRootData")]
        //[ApiActionPermission(ActionPermissionOptions.Search)]
        //public ActionResult GetCatalogRootData([FromBody] PageDataOptions options)
        //{
        //    //页面加载(一级)根节点数据条件x => x.ParentId==null,自己根据需要设置
        //    var query = equip_devcatalogRepository.Instance.FindAsIQueryable(x => x.ParentId == null);

        //    var rows = query.TakeOrderByPage(options.Page, options.Rows)
        //        .OrderBy(x => x.DevCatalogName).Select(s => new
        //        {
        //            s.DevCatalogId,
        //            s.DevCatalogName,
        //            s.DevCatalogCode,
        //            s.ParentId,
        //            s.Enable,
        //            s.Remark,
        //            s.CreateID,
        //            s.Creator,
        //            s.CreateDate,
        //            s.ModifyID,
        //            s.Modifier,
        //            s.ModifyDate,
        //            hasChildren = true
        //        }).ToList();
        //    return JsonNormal(new { total = query.Count(), rows });
        //}

        ///// <summary>
        /////treetable 获取子节点数据
        ///// </summary>
        ///// <returns></returns>
        //[HttpPost, Route("getChildrenData")]
        //[ApiActionPermission(ActionPermissionOptions.Search)]
        //public async Task<ActionResult> GetChildrenData(string devCatalogId)
        //{
        //    //点击节点时，加载子节点数据
        //    var devRepository = equip_devcatalogRepository.Instance.FindAsIQueryable(x => 1 == 1);   var rows = await devRepository.Where(x => x.ParentId == devCatalogId)
        //        .Select(s => new
        //        {
        //            s.DevCatalogId,
        //            s.DevCatalogName,
        //            s.DevCatalogCode,
        //            s.ParentId,
        //            s.Enable,
        //            s.Remark,
        //            s.CreateID,
        //            s.Creator,
        //            s.CreateDate,
        //            s.ModifyID,
        //            s.Modifier,
        //            s.ModifyDate,
        //            hasChildren = devRepository.Any(x => x.ParentId == s.DevCatalogId)
        //        }).ToListAsync();
        //    return JsonNormal(new { rows });
        //}

    }
}
